﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace TlvLib.SiChuan.Unpacker
{
    class TimeSyncReplyUnpacker
    {
        public static bool TryUnpack(byte[] buffer, out DateTime time)
        {
            time = DateTime.MinValue;

            if (!Helper.IsValid(buffer))
            {
                return false;
            }

            if (Helper.GetPacketType(buffer) != PacketType.TimeSync)
            {
                return false;
            }

            //var timeStrLen = Helper.CountUptoWhiteSpace(buffer, 8, 24);
            var timeString = Configuration.TextEncoding.GetString(buffer, 8, 23);
            return DateTime.TryParseExact(timeString, ProtocolConstant.TimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out time);
        }
    }
}
